/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/10/22
 */
layui.define(['form', 'layedit', 'zmnBind'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    let zmnBind = layui.zmnBind;

    form.render(null, "zmn-form");
    zmnLayUtil.layFormVerify(form);

    // 组织架构的页面domID
    let orgInputIds = ['#plat', '#orgTwoId', '#orgId'];

    // 部门
    let bindDepts = function () {
        zmnBind.selectl(form, json.ZMN_URL_ENV + "mcc.xiujiadian.com" + '/common/dept/listChilds', 'deptOneId', 'deptTwoId', 'deptId', [json.deptOneId, json.deptTwoId, json.deptId], [function () {
            return {orgId: findInputValue(orgInputIds), parentId: 1}
        }, function () {
            return {orgId: findInputValue(orgInputIds), parentId: zmnUtil.emptyToNull($("#deptOneId").val())}
        }, function () {
            return {orgId: findInputValue(orgInputIds), parentId: zmnUtil.emptyToNull($("#deptTwoId").val())}
        }], null, false);
    };

    // 组织架构
    zmnBind.selectl(form, json.ZMN_URL_ENV + "mcc.xiujiadian.com" + '/common/organize/listChilds', 'plat', 'orgTwoId', 'orgId', [json.plat, json.orgTwoId, json.orgId], [function () {
        return {parentId: 1}
    }, function () {
        return {parentId: zmnUtil.emptyToNull($("#plat").val())}
    }, function () {
        return {parentId: zmnUtil.emptyToNull($("#orgTwoId").val())}
    }], [
        function () {
            bindDepts();
        },
        function () {
            bindDepts();
        },
        function () {
            bindDepts();
        }
    ], null, false);

    bindDepts();

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        var openId = layer.open({type: 3});
        var data = obj.field;
        var url = "/notice/addEdit.action";

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(result) {
                layer.close(openId);
                if(result.status == 200) {
                    layer.msg(result.message,{time: 1000},()=>{
                        zmnLayUtil.closeThisIframe(parent);
                        parent.location.reload();
                    });
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                layer.close(openId);
            }
        })
        return false;
    });

    layui.$("#cancleBtn").click(function () {
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
        return false;
    });
    //对外暴露的接口
    exports('notice_detail_js', {});
});

/**
 * 从多个input中获取input的值，以最后一个input优先
 * @param selectors
 * @returns {number|jQuery}
 */
function findInputValue(selectors) {
    if (!zmnUtil.isArray(selectors)) {
        return 0;
    }
    let l = selectors.length;
    for (let i = l - 1; i >= 0; i--) {
        let $this = $(selectors[i]);
        if ($this.length > 0 && zmnUtil.isNotZeroInteger($this.val())) {
            return $this.val();
        }
    }
    return 0;
}

